/*
 * Сортировка Шелла
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include <limits.h>

/* {{{ memory allocation wrappers */
extern void *s_calloc(size_t n, size_t size)
{
	void *ptr;
	ptr = calloc(n, size);
	assert(ptr != NULL);
	if (ptr == NULL) {
		fprintf(stderr, "%s(): out of memory, failed to allocate %lu bytes\n",
				__func__, n * size);
		abort();
	}
	return ptr;
}

extern void *s_malloc(size_t size)
{
	void *ptr;
	ptr = malloc(size);
	assert(ptr != NULL);
	if (ptr == NULL) {
		fprintf(stderr, "%s(): out of memory, failed to allocate %lu bytes\n",
				__func__, size);
		abort();
	}
	return ptr;
}

extern void *s_realloc(void *oldptr, size_t newsize)
{
	void *newptr;
	newptr = realloc(oldptr, newsize);
	if (newptr == NULL) {
		fprintf(stderr, "%s(): out of memory, "
				"failed to realloc %p for new size of %lu bytes\n",
				__func__, oldptr, newsize);
		abort();
	}
	return newptr;
}
/* }}} */

long long
ceil(long long num, long long den)
{
	register long long ans = num / den;
	while (ans * den < num)
		ans++;
	return ans;
}

int
Hibbard(int n)
{
	return (1 << n) - 1;
}

int
Sedgewick(int n)
{
	if (n == 1)
		return 1;
	--n;
	return (1 << (2 * n)) + 3 * (1 << (n - 1)) + 1;
}

int
Tokuda(int n)
{
	register long long r = 1;
	int i;

	for (i = 0; i < n; i++)
		r *= 9;

	return (int) ceil(r - (1LL << (n * 2)), 5LL * (1LL << (2 * (n - 1))));
}

#if 0
static int (*gap_generators[3])(int) = {
	Hibbard,
	Sedgewick,
	Tokuda
};
#endif

#define SWAP_INT(a, b)	(((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b)))
int
shellsort(int *array, const int n, int (*gap_gen)(int))
{
	int gap, i, k;
	int comprs = 0;
	int q;

	for (gap = gap_gen(q = 1); gap < n; gap = gap_gen(++q));
	--q;
	while (q > 0 && gap > 0) {
		gap = gap_gen(q--);
		for (i = gap; i < n; i++) {
			for (k = i - gap; k >= 0 && array[k] > array[k + gap];
					k -= gap) {
				++comprs;
				SWAP_INT(array[k], array[k + gap]);
			}
			if (k >= 0)
				++comprs;
		}
	}

	return comprs;
}
#undef SWAP_INT

int
main(int argc, char **argv)
{
	int n, mod, a, b;
	register int i = 0;
	int *seq;
	int *buf;

	scanf("%d %d", &n, &mod);
	seq = s_malloc(n * sizeof *seq);
	scanf("%d %d %d", &seq[0], &a, &b);
	for (i = 1; i < n; i++)
		seq[i] = (a * seq[i - 1] + b) % mod;

	buf = s_malloc(n * sizeof *buf);
	memcpy(buf, seq, n * sizeof seq[0]);
	printf("%d ", shellsort(buf, n, Hibbard));
#ifdef CHK
	for (i = 1; i < n; i++) {
		if (buf[i] < buf[i - 1]) {
			fprintf(stderr, "%s: inv A[%d]=%d, A[%d]=%d\n",
					"Hibbard", i - 1, buf[i - 1], i, buf[i]);
		}
	}
#endif
	memcpy(buf, seq, n * sizeof seq[0]);
	printf("%d ", shellsort(buf, n, Sedgewick));
#ifdef CHK
	for (i = 1; i < n; i++) {
		if (buf[i] < buf[i - 1]) {
			fprintf(stderr, "%s: inv A[%d]=%d, A[%d]=%d\n",
					"Sedgewick", i - 1, buf[i - 1], i, buf[i]);
		}
	}
#endif
	printf("%d\n", shellsort(seq, n, Tokuda));
#ifdef CHK
	for (i = 1; i < n; i++) {
		if (seq[i] < seq[i - 1]) {
			fprintf(stderr, "%s: inv A[%d]=%d, A[%d]=%d\n",
					"Tokuda", i - 1, seq[i - 1], i, seq[i]);
		}
	}
#endif

	free(seq);
	free(buf);
	return 0;
}
